package crudfx.data;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
/*public class TheString {
        public var name:String;
        public var value:String="";
    };
*/
/**
 * @profile desktop
 * @author Sergey Surikov
 */
/*class InnerValue{
        public var name:String="";
        public var value:String="";
        public var group:String="";
    }*/
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class GroupProperties {
    public var current:String="";
    public var dataTree:DataTree;
    var strings:NamedValue[];
    var values:GroupValue[];
    function choose(key:String,lang:String):String{
        for(t:GroupValue in values){
            if(t.name.equals(key) and t.group.equals(lang)){
                return t.value;
            }
        };
        var i:GroupValue=GroupValue{
            name:key
            group:current
            value:"?{key}/{current}"
        }
        insert i into values;
        return i.value;
    }
    public function get(key:String):NamedValue{
        for(t in strings){
            if(t.name.equals(key)){
                return t;
            }
        };
        for(t in values){
            if(t.name.equals(key) and t.group.equals(current)){
                var v:NamedValue=NamedValue{
                    name:key
                    value:bind choose(v.name,current);
                };
                insert v into strings;
                return v;
            }
        }
        var i:GroupValue=GroupValue{
            name:key
            group:current
            value:"?{key}/{current}"
        }
        insert i into values;
        var newValue:NamedValue=NamedValue{
            name:key
            value:bind choose(newValue.name,current);
        };
        insert newValue into strings;
        return newValue;
    }
    public function gather():Void{
        for(t:GroupValue in values){
            if(t.name!="" and t.group!=""){
                if(t.name.length() > 0){
                    dataTree.node(t.name).node(t.group).asString("").value=t.value;
                }
            }
        };
    }
    postinit{
        for(word:DataTree in dataTree.children){
            for(group:DataTree in word.children){
                if(word.name!="" and group.name!=""){
                    insert GroupValue{
                        name:word.name
                        group:group.name
                        value:group.asString("").value;
                        }  into values;
                }
            }
        }
    }
}
